package cqrtplm.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.annotion.CustomListenerField;
import com.hustcad.plm.pdm.workflow.model.constant.ProcessConstant;
import cqrtplm.service.RTChangeNoticeService;
import cqrtplm.service.TaskSubmitVerfiyService;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.impl.el.FixedValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@CustomListener(category = "润通", description = "任务节点控制监听")
public class RTNodeVerifyListener implements ExecutionListener {
    private static final Logger log = LoggerFactory.getLogger(RTNodeVerifyListener.class);
    @CustomListenerField(
            description = "任务名称-显示名称",
            defaultValue = "",
            required = false
    )
    private FixedValue task;

    @Override
    public void notify(DelegateExecution execution) {
        long start = System.currentTimeMillis();
        String workFlowContainerId = execution.getProcessInstanceBusinessKey();
        log.info(
                "----------润通----------开始任务节点控制监听------workFlowContainerId:{}----->>>",
                workFlowContainerId);
        if (StringUtils.isBlank(workFlowContainerId)) {
            workFlowContainerId = execution.getVariable(ProcessConstant.WORK_FLOW_CONTAINER_ID).toString();
        }
        if (this.task == null) {
            log.info("检查变更对象状态，监听参数targetStatusKey为空，请给对应监听参数设置值！");
        } else {
            TaskSubmitVerfiyService taskSubmitVerfiyService = SpringUtil.getBean(TaskSubmitVerfiyService.class);
            taskSubmitVerfiyService.taskSubmitVerfiy(workFlowContainerId, this.task.getExpressionText());
        }
        log.info("----------任务节点控制监听：{}---->>>", System.currentTimeMillis() - start);
    }


}
